package com.jsptpd.smartauth.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by LOG on 2016/7/12.
 */
public class HashUtils {

    public static String md5(String data) throws NoSuchAlgorithmException {

        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(data.getBytes());

        StringBuffer buf = new StringBuffer();

        byte[] bits = md.digest();

        for(int i=0;i<bits.length;i++){

            int a = bits[i];

            if(a<0) a+=256;

            if(a<16) buf.append("0");

            buf.append(Integer.toHexString(a));

        }

        return buf.toString();

    }

    public static String sha1(String data)   {

        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");

            md.update(data.getBytes());

            StringBuffer buf = new StringBuffer();

            byte[] bits = md.digest();

            for (int i = 0; i < bits.length; i++) {

                int a = bits[i];

                if (a < 0) a += 256;

                if (a < 16) buf.append("0");

                buf.append(Integer.toHexString(a));
            }
            return buf.toString();
        }
        catch(Exception e){
            return "";
        }


    }

    public static String shaPwd(String data) {
        return sha1(data);
    }

}
